home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / Delphi 3.0 / DATA.Z / syncobjs.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-01-29  |  2.8 KB  |  146 lines

  1. unit SyncObjs;
  2.  
  3. interface
  4.  
  5. uses Sysutils, Windows, Messages, Classes;
  6.  
  7. type
  8.   TSynchroObject = class(TObject)
  9.   public
  10.     procedure Aquire; virtual;
  11.     procedure Release; virtual;
  12.   end;
  13.  
  14.   THandleObject = class(TSynchroObject)
  15.   private
  16.     FHandle: THandle;
  17.     FLastError: Integer;
  18.   public
  19.     destructor Destroy; override;
  20.     property LastError: Integer read FLastError;
  21.     property Handle: THandle read FHandle;
  22.   end;
  23.  
  24.   TWaitResult = (wrSignaled, wrTimeout, wrAbandoned, wrError);
  25.  
  26.   TEvent = class(THandleObject)
  27.   public
  28.     constructor Create(EventAttributes: PSecurityAttributes; ManualReset,
  29.       InitialState: Boolean; const Name: string);
  30.     function WaitFor(Timeout: Longint): TWaitResult;
  31.     procedure SetEvent;
  32.     procedure ResetEvent;
  33.   end;
  34.  
  35.   TSimpleEvent = class(TEvent)
  36.   public
  37.     constructor Create;
  38.   end;
  39.  
  40.   TCriticalSection = class(TSynchroObject)
  41.   private
  42.     FSection: TRTLCriticalSection;
  43.   public
  44.     constructor Create;
  45.     destructor Destroy; override;
  46.     procedure Aquire; override;
  47.     procedure Release; override;
  48.     procedure Enter;
  49.     procedure Leave;
  50.   end;
  51.  
  52. implementation
  53.  
  54. { TSynchroObject }
  55.  
  56. procedure TSynchroObject.Aquire;
  57. begin
  58. end;
  59.  
  60. procedure TSynchroObject.Release;
  61. begin
  62. end;
  63.  
  64. { THandleObject }
  65.  
  66. destructor THandleObject.Destroy;
  67. begin
  68.   CloseHandle(FHandle);
  69.   inherited Destroy;
  70. end;
  71.  
  72. { TEvent }
  73.  
  74. constructor TEvent.Create(EventAttributes: PSecurityAttributes; ManualReset,
  75.   InitialState: Boolean; const Name: string);
  76. begin
  77.   FHandle := CreateEvent(EventAttributes, ManualReset, InitialState, PChar(Name));
  78. end;
  79.  
  80. function TEvent.WaitFor(Timeout: Longint): TWaitResult;
  81. begin
  82.   case WaitForSingleObject(Handle, Timeout) of
  83.     WAIT_ABANDONED: Result := wrAbandoned;
  84.     WAIT_OBJECT_0: Result := wrSignaled;
  85.     WAIT_TIMEOUT: Result := wrTimeout;
  86.     WAIT_FAILED:
  87.       begin
  88.         Result := wrError;
  89.         FLastError := GetLastError;
  90.       end;
  91.   end;
  92. end;
  93.  
  94. procedure TEvent.SetEvent;
  95. begin
  96.   Windows.SetEvent(Handle);
  97. end;
  98.  
  99. procedure TEvent.ResetEvent;
  100. begin
  101.   Windows.ResetEvent(Handle);
  102. end;
  103.  
  104. { TSimpleEvent }
  105.  
  106. constructor TSimpleEvent.Create;
  107. begin
  108.   FHandle := CreateEvent(nil, True, False, nil);
  109. end;
  110.  
  111. { TCriticalSection }
  112.  
  113. constructor TCriticalSection.Create;
  114. begin
  115.   inherited Create;
  116.   InitializeCriticalSection(FSection);
  117. end;
  118.  
  119. destructor TCriticalSection.Destroy;
  120. begin
  121.   DeleteCriticalSection(FSection);
  122.   inherited Destroy;
  123. end;
  124.  
  125. procedure TCriticalSection.Aquire;
  126. begin
  127.   EnterCriticalSection(FSection);
  128. end;
  129.  
  130. procedure TCriticalSection.Release;
  131. begin
  132.   LeaveCriticalSection(FSection);
  133. end;
  134.  
  135. procedure TCriticalSection.Enter;
  136. begin
  137.   Aquire;
  138. end;
  139.  
  140. procedure TCriticalSection.Leave;
  141. begin
  142.   Release;
  143. end;
  144.  
  145. end.
  146.